import http from '@/common/http';
import Loading from '@/components/loading';
import Utils from '@/common/pakjUtils';
import Config from '@/common/config';
export default {
  wxConfig(apilist = ['chooseWXPay']) {
    return new Promise((resolve, reject) => {
      Loading.show();
      let params = {
        url: location.href
      };
      if (apilist instanceof Array) {
        apilist.push('hideMenuItems');
      }
      http
        .post('pakj/common/getWxConfig', params)
        .then(res => {
          Loading.hide();
          let config = res.data.data;
          wx.config({
            debug: false,
            appId: config.appId,
            timestamp: config.timestamp,
            nonceStr: config.nonceStr,
            signature: config.signature,
            jsApiList: apilist
            // [
            //     'chooseWXPay',
            //     'checkJsApi',
            //     'onMenuShareTimeline',
            //     'onMenuShareAppMessage',
            //     'onMenuShareQQ',
            //     'onMenuShareWeibo',
            //     'onMenuShareQZone',
            //     'hideMenuItems',
            //     'showMenuItems',
            //     'hideAllNonBaseMenuItem',
            //     'showAllNonBaseMenuItem',
            //     'translateVoice',
            //     'startRecord',
            //     'stopRecord',
            //     'onVoiceRecordEnd',
            //     'playVoice',
            //     'onVoicePlayEnd',
            //     'pauseVoice',
            //     'stopVoice',
            //     'uploadVoice',
            //     'downloadVoice',
            //     'chooseImage',
            //     'previewImage',
            //     'uploadImage',
            //     'downloadImage',
            //     'getNetworkType',
            //     'openLocation',
            //     'getLocation',
            //     'hideOptionMenu',
            //     'showOptionMenu',
            //     'closeWindow',
            //     'scanQRCode',
            //     'chooseWXPay',
            //     'openProductSpecificView',
            //     'addCard',
            //     'chooseCard',
            //     'openCard'
            // ]
          });
          wx.ready(() => {
            wx.hideMenuItems({
              menuList: ['menuItem:share:QZone'] // 要隐藏的菜单项，只能隐藏“传播类”和“保护类”按钮，所有menu项见附录3
            });
            Loading.hide();
            resolve(res);
          });
          wx.error(res => {
            Loading.hide();
            reject(new Error('微信配置错误'));
          });
        })
        .catch(reason => {
          Loading.hide();
          reject(new Error('无法获取微信配置'));
        });
    });
  },
  getCurrentLocation() {
    return new Promise((resolve, reject) => {
      wx.getLocation({
        type: 'gcj02',
        success: function(res) {
          console.log(res);
          resolve(res);
        },
        cancel: function(res) {
          console.log(res);
          reject(new Error('无法获取当前位置'));
        },
        fail: function(res) {
          console.log('res');
          reject(new Error(res.errMsg));
        }
      });
    });
  },
  startWxAuth(state = 'wxauth1') {
    const env = Utils.getCurrentEnv();
    const query = Utils.parseQueryString();
    if (env === 'wx') {
      if (query && query.code && query.state) {
        return;
      }
      console.error('微信|小程序环境下没有检测到openid以及unionid');
      let origin = window.location.origin;
      let returnUrl = origin + '/splash';
      let param = {
        appid: Config.appid,
        redirect_uri: encodeURIComponent(returnUrl),
        response_type: 'code',
        scope: 'snsapi_userinfo',
        state: state
      };
      let arr = [];
      for (var key in param) {
        let str = key + '=' + param[key];
        arr.push(str);
      }
      let paramStr = arr.join('&');
      window.location.href =
        Config.wxauth + '?' + paramStr + '#wechat_redirect';
    }
  },
  restartMiniProgram() {
    if (Utils.isMiniProgram()) {
      wx.miniProgram.reLaunch({
        url: '/pages/index/index'
      });
    }
  }
};
